/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.basic.park.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.basic.park.dto.BaseSafeManageMaterialExcelDTO;
import com.hopes.bizz.basic.park.entity.BaseSafeManageMaterialEntity;
import com.hopes.bizz.basic.park.service.BaseSafeManageMaterialService;
import com.hopes.bizz.basic.park.vo.BaseSafeManageMaterialExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.core.util.StringUtils;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 安全管理体系资料
 *
 * @author pig
 * @date 2023-10-23 19:00:28
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/safe")
@Tag(description = "safe", name = "安全管理体系资料管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BaseSafeManageMaterialController {

    private final BaseSafeManageMaterialService baseSafeManageMaterialService;

    /**
     * 分页查询
     *
     * @param page                   分页对象
     * @param baseSafeManageMaterial 安全管理体系资料
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('park_safe_view')")
    public R getbaseSafeManageMaterialPage(Page page, BaseSafeManageMaterialEntity baseSafeManageMaterial) {
        LambdaQueryWrapper<BaseSafeManageMaterialEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(baseSafeManageMaterial.getMaterialName()), BaseSafeManageMaterialEntity::getMaterialName, baseSafeManageMaterial.getMaterialName())
                .orderByDesc(BaseSafeManageMaterialEntity::getShowFlag);
        baseSafeManageMaterial.setMaterialName(null);
        wrapper.setEntity(baseSafeManageMaterial);
        return R.ok(baseSafeManageMaterialService.page(page, wrapper));
    }


    /**
     * 通过id查询安全管理体系资料
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('park_safe_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(baseSafeManageMaterialService.getById(id));
    }

    /**
     * 新增安全管理体系资料
     *
     * @param baseSafeManageMaterial 安全管理体系资料
     * @return R
     */
    @Operation(summary = "新增安全管理体系资料", description = "新增安全管理体系资料")
    @SysLog("新增安全管理体系资料")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('park_safe_add')")
    public R save(@RequestBody BaseSafeManageMaterialEntity baseSafeManageMaterial) {
        return R.ok(baseSafeManageMaterialService.save(baseSafeManageMaterial));
    }

    /**
     * 修改安全管理体系资料
     *
     * @param baseSafeManageMaterial 安全管理体系资料
     * @return R
     */
    @Operation(summary = "修改安全管理体系资料", description = "修改安全管理体系资料")
    @SysLog("修改安全管理体系资料")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('park_safe_edit')")
    public R updateById(@RequestBody BaseSafeManageMaterialEntity baseSafeManageMaterial) {
        return R.ok(baseSafeManageMaterialService.updateById(baseSafeManageMaterial));
    }

    /**
     * 通过id删除安全管理体系资料
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除安全管理体系资料", description = "通过id删除安全管理体系资料")
    @SysLog("通过id删除安全管理体系资料")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('park_safe_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(baseSafeManageMaterialService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('park_safe_export')")
    public List<BaseSafeManageMaterialExcelVO> template() {
        return baseSafeManageMaterialService.exportExcel(Collections.singletonList(new BaseSafeManageMaterialEntity()));
    }

    /**
     * 导出excel 表格
     *
     * @param baseSafeManageMaterialExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('park_safe_export')")
    public List<BaseSafeManageMaterialExcelVO> export(BaseSafeManageMaterialExcelDTO baseSafeManageMaterialExcelDto) {
        LambdaQueryWrapper<BaseSafeManageMaterialEntity> wrapper = Wrappers.lambdaQuery(BaseSafeManageMaterialEntity.class)
                .eq(StringUtils.isNotBlank(baseSafeManageMaterialExcelDto.getMaterialType()),
                        BaseSafeManageMaterialEntity::getMaterialType, baseSafeManageMaterialExcelDto.getMaterialType())
                .like(StringUtils.isNotBlank(baseSafeManageMaterialExcelDto.getMaterialName()),
                        BaseSafeManageMaterialEntity::getMaterialName, baseSafeManageMaterialExcelDto.getMaterialName());
        return baseSafeManageMaterialService.exportExcel(baseSafeManageMaterialService.list(wrapper));
    }

    /**
     * 导入excel 表格
     *
     * @param baseSafeManageMaterialExcelDtoList 查询条件
     * @param bindingResult                      错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('park_safe_export')")
    public R importExcel(@RequestExcel List<BaseSafeManageMaterialExcelDTO> baseSafeManageMaterialExcelDtoList, BindingResult bindingResult) {
        return baseSafeManageMaterialService.importExcel(baseSafeManageMaterialExcelDtoList, bindingResult);
    }

    @Operation(summary = "通过id修改gis展示的数据", description = "通过id修改gis展示的数据")
    @SysLog("通过id修改gis展示的数据")
    @PutMapping("showGis")
    @PreAuthorize("@pms.hasPermission('park_safe_edit')")
    public R showGis(@RequestBody Map<String,Long> params) {
        baseSafeManageMaterialService.updateShowGis(params.get("id"));
        return R.ok();
    }
}
